<?php

namespace App\Logics;


use App\Models\User;
use App\Models\UserAuth;
use App\Service\Base\Logic;

class AuthLogic extends Logic
{

    // 详情
    public function auth($uid)
    {
        // 认证以前请先完善资料
        $userDb = new User();
        $info   = $userDb->select('id', 'verify', 'is_apply', 'mobile', 'nickname', 'auth_way')
                         ->where('id', $uid)->first();
        $mobile = $info['mobile'];
        $this->throwIf(empty($mobile), '请先完善个人资料！');
        $this->throwIf(empty($code), '请填写验证码！');
        // 判断是否已经提交认证
        //$this->throwIf($info['verify'] == 1, '你已经通过认证审核！');
        //$this->throwIf($info['is_apply'], '你已经申请认证,请等待审核！');

        $links    = $this->getData('links', [], 'is_array');
        $files    = $this->getData('files', [], 'is_array');
        $areas    = $this->getData('areas', [], 'is_array');
        $code     = $this->getData('code', '', 'trim');
        $_name    = $this->getData('auth_name', null, 'trim');
        $_type    = $this->getData('auth_type', null, 'trim');
        $_city    = $this->getData('city', null, 'trim');
        $_address = $this->getData('address', null, 'trim');
        $_desc    = $this->getData('description', null, 'trim');
        $_license = $this->getData('license_img', null, 'trim');
        $_school  = $this->getData('school_allow', null, 'trim');
        //$this->throwIf(empty($city), '城市不能为空！');
        //$this->throwIf(count($files) != count($files, 1), '请传递正确文件地址！');
        //$this->throwIf(count($links) != count($links, 1), '请传递正确网络连接！');
        //$this->throwIf(count($areas) != count($areas, 1), '请传递正确网络擅长领域！');
        $this->throwIf(empty($_type), '请选择认证类型！');


        // 判断是否已经提交（可能重复提交） ->where('verify', 0)
        $verifyDb = new UserVerify();
        $verInfo  = $userDb->where('uid', $uid)->first();
        $this->throwIf($verInfo && $verInfo['verify'], '你已经申请认证！');
        $this->throwIf($verInfo && ($verInfo['verify'] == 0), '你已经申请认证,请等待审核！');

        $data = [
            'uid'          => $uid,
            'auth_type'    => $_type,
            'auth_name'    => $_name,
            'city'         => $_city,
            'address'      => $_address,
            'description'  => $_desc,
            'license_img'  => $_license,
            'school_allow' => $_school,
        ];

        // 判断验证码
        //$smsDb = new SiteSms();
        //$where = ['mobile' => $mobile, 'code' => $code, "status" => "0"];
        //$sinfo = $smsDb->where($where)->orderBy("id", "DESC")->first();
        //$this->throwIf(empty($sinfo), '验证码错误！！');
        //$this->throwIf(LARAVEL_TIME - $sinfo->ctime > 600, '验证码超时！！');

        // 验证信息
        $rules  = ["apply_type1" => 'required', "apply_type2" => 'required', "realname" => 'required', "position" => 'required', "areas" => 'required', "files" => 'required', "job_time" => 'required',];
        $msg    = ["apply_type1" => '身份类型', "apply_type2" => '身份类型', "realname" => '真实姓名', "position" => '头衔', "areas" => '擅长领域', "description" => '描述', "files" => '证明文件', "job_time" => '从业年限',];
        $result = $userDb->validator($data, $rules, $msg);
        if($result === false){
            return $this->error($userDb->getError());
        }
        $this->throwReturn($data['areas'] == '[]', '请选择你你擅长的领域！');
        $this->throwReturn($data['files'] == '[]', '请上传证明文件！');

        // 是否已经认证
        //$verifyInfo = $verifyDb->isCache(true)->oneFind(['uid' => $data['uid']]);

        // 审核内容
        //$this->baiduTextCensor($data);

        // 保存
        $authDb = new UserAuth();
        $res    = $authDb->auth($uid, $data);
        $this->throwIf($res === false, '你已经申请认证,请等待审核！');

        return $this->success([], '认证提交成功，请等待审核！');
    }

}


